import { graduationCycle } from "../../utils/config"
import { PicVideoProps } from "../../utils/type"

// components/class/class.ts
Component({

  /**
   * 组件的属性列表
   */
  properties: {
    data: {
      type: Object,
      value: {
        className: ''
      },
      observer(newVal, oldVal) {
        // console.log('class detail', newVal)
        if (newVal) {
          const arr: PicVideoProps[] = []
          newVal.classVideo.forEach((item: any) => {
            arr.push({
              type: 'video',
              url: item.videoUrl,
              coverUrl: item.coverUrl,
              tag: item.tag
            })
          })
          newVal.classPic.forEach((item: any) => {
            arr.push({
              type: 'image',
              url: item.picUrl,
              tag: item.tag
            })
          })

          let time = 0, i = 0
          graduationCycle.forEach((item, index: number) => {
            if (item.classNumber === newVal.classNumber) {
              time = item.time
              i = index
            }
          })

          this.setData({
            picAndVideo: arr,
            classDetail: newVal,
            time,
            periodIndex: i,
            classNumber: newVal.classNumber
          })
        }
      }
    }
  },

  /**
   * 组件的初始数据
   */
  data: {
    tag: <string[]>[],
    picAndVideo: <PicVideoProps[]>[],
    classDetail: <any>{},
    periodIndex: 0,
    time: 0,
    classNumber: '',
    themeCode: ''
  },
  lifetimes: {
    attached() {
      //从本地获取当前选择的城市编码
      this.setData({
        themeCode: wx.getStorageSync('themeCode')
      })
    }
  },

  /**
   * 组件的方法列表
   */
  methods: {
    //打开对应的页面
    onOpenPage(res: any) {
      console.log(res.currentTarget.dataset)
      const dataset = res.currentTarget.dataset

      if (dataset.pathType === 'introduce') {
        const introduce = {
          tag: dataset.tag,
          type: dataset.type,
          content: '',
          url: dataset.url,
          poster: dataset.poster,
        }
        if (dataset.tag === '班别视频') {
          introduce.content = this.data.classDetail.classVideoIntroduce
        } else if (dataset.tag === '课程内容') {
          introduce.content = this.data.classDetail.courseContent
        } else if (dataset.tag === '费用清单') {
          introduce.content = this.data.classDetail.feeList
        } else if (dataset.tag === '学习流程') {
          introduce.content = this.data.classDetail.studyFlow
        } else if (dataset.tag === '退款说明') {
          introduce.content = this.data.classDetail.refundExplain
        }
        wx.setStorageSync('introduce', introduce)
      } else if (dataset.pathType === 'detail') {
        wx.setStorageSync('classDetail', this.data.classDetail)
      }

      wx.navigateTo({ url: res.currentTarget.dataset.path })
    }
  }
})